/*
 * HomePage.java
 *
 * Created on January 25, 2012, 10:34 AM
 */

package com.myapp.wicket;           
import Entities.*;
import DataLayer.*;
import java.util.*;
import org.apache.wicket.markup.html.basic.Label;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

import org.apache.wicket.PageParameters;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.extensions.model.AbstractCheckBoxModel;
import org.apache.wicket.markup.html.form.Form;

public class HomePage extends WebPage implements AuthenticatedWebPage {

    private Set<Product> selected = new HashSet<Product>();
    private DataRepository repository=new DataRepository();
   
    private static List<Product> products = null;
    
    
     public HomePage() {

                 //getting data from db
        products=repository.allProducts();
      
       // add(new FeedbackPanel("feedback"));
        
        //generating columns
        List<IColumn<Product>> columns = new ArrayList<IColumn<Product>>();
        columns.add(new CheckBoxColumn<Product>() 
        {
            @Override
			protected IModel<Boolean> newCheckBoxModel(final IModel<Product> rowModel) 
                        {
				        return new AbstractCheckBoxModel() 
                                        {@Override
					public void unselect() {
						selected.remove(rowModel.getObject());
					}

					@Override
					public void select() {
						selected.add(rowModel.getObject());
					}

					@Override
					public boolean isSelected() {
						return selected.contains(rowModel.getObject());
					}

					@Override
					public void detach() {
						rowModel.detach();
					}
				};
			}

	});
        columns.add(new PropertyColumn<Product>(Model.of("Id"), "id"));
        columns.add(new PropertyColumn<Product>(Model.of("Name"), "name","name"));
	columns.add(new PropertyColumn<Product>(Model.of("Description"), "description"));
        columns.add(new PropertyColumn<Product>(Model.of("Current Price"), "currentPrice","currentPrice"));
        columns.add(new PropertyColumn<Product>(Model.of("Current Byer"), "currentUser"));
        columns.add(new PropertyColumn<Product>(Model.of("Min Step"), "minStep"));
        Form<?> form = new Form<Void>("form") {
			@Override
			protected void onSubmit() 
                        {
                            String ids="";
				for (Product product : selected) {
                                     ids+=product.id;   
                                }
                                if (ids.length()>0)
                                {
                                PageParameters pageParameters = new PageParameters();
                                pageParameters.add("id",ids);
                              //  setResponsePage(OrderConfirmationPage.class, pageParameters);  
                                }
			}
         };
        add(form);
        form.add(new DefaultDataTable<Product>("products", columns, new ProductsProvider(products), 10));
    
    }

}
